/**
 * 
 */
package cn.sundat.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import cn.sundat.model.PageBean;
import cn.sundat.model.School;
import cn.sundat.model.School_link;

/**
 * @author XIN
 *
 */
public class SchoolDaoImpl extends HibernateDaoSupport implements SchoolDao{

	@Override
	public void addSchool_link(School_link school_link) {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(school_link);
	}

	@Override
	public void delSchool_link(School_link school_link) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(school_link);
	}

	@Override
	public void editSchool_link(School_link school_link) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(school_link);
	}

	@Override
	public List<School> findAllSchool() {
		// TODO Auto-generated method stub
		DetachedCriteria criteria=DetachedCriteria.forClass(School.class);
		return (List<School>) getHibernateTemplate().findByCriteria(criteria);
	}

	@Override
	public void addSchool(School school) {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(school);
	}

	@Override
	public void delSchool(School school) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(school);
	}

	@Override
	public void editSchool(School school) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(school);
	}

	@Override
	public School findSchoolById(String id) {
		// TODO Auto-generated method stub
		List<School> findList = (List<School>) getHibernateTemplate().find("from School where sch_id=?", id);
		return findList.size()>0?findList.get(0):null;
	}
	@Override
	public PageBean<School> findByPageByKey(Integer currPage,Integer pageSize,String key) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(School.class);
		criteria.add(Restrictions.like("sch_name", key,MatchMode.ANYWHERE));
		PageBean<School> pageBean=(PageBean<School>) new PageBean<>(currPage,pageSize,count(key),getHibernateTemplate().findByCriteria(criteria, (currPage-1)*pageSize,pageSize));
		return pageBean;
	}
	public Integer count(String key) {
		return ((Long) getHibernateTemplate().iterate("select count(*) from School where sch_name like '%"+key+"%'").next()).intValue();
	}

	@Override
	public List<School_link> findSchool_linkById(String id) {
		// TODO Auto-generated method stub
		List<School_link> findList = (List<School_link>) getHibernateTemplate().find("from School_link where sch_id=?", id);
		return findList.size()>0?findList:null;
	}
	@Override
	public List<School> findByKey(String key) {
		DetachedCriteria criteria=DetachedCriteria.forClass(School.class);
		criteria.add(Restrictions.like("sch_name", key,MatchMode.ANYWHERE));
		List<School> schools = (List<School>) getHibernateTemplate().findByCriteria(criteria);
		return schools;
	}

	@Override
	public School_link findSchool_linkBySchlink_id(String schlink_id) {
		// TODO Auto-generated method stub
		School_link school_link = (School_link) getHibernateTemplate().find("from School_link where schlink_id=?", schlink_id).get(0);
		return school_link;
	}

}
